package com.yeqian.user.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yeqian.entity.ResultJson;
import com.yeqian.user.VO.UserInfoVO;
import com.yeqian.user.entity.UserInfo;
import com.yeqian.user.mapper.UserInfoMapper;
import com.yeqian.user.service.RoleInfoService;
import com.yeqian.user.service.UserInfoService;
import com.yeqian.user.utils.BiliBiliUtil;
import com.yeqian.vo.PageVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Resource
    private RoleInfoService roleInfoService;

    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 通过名字获取用户信息
     *
     * @param username
     * @return
     */
    @Override
    public UserInfo getUserByName(String username) {
        return userInfoMapper.getUserByName(username);
    }

    /**
     * 通过名字获取用户的所有信息（角色，权限）
     *
     * @param username
     * @return
     */
    @Override
    public UserInfoVO getUserInfoDetailByName(String username) {
        UserInfoVO userInfoVO = new UserInfoVO();
        UserInfo userByName = this.getUserByName(username);
        //设置用户信息
        userInfoVO.setUserInfo(userByName);
        List<String> userRoleById = roleInfoService.getUserRoleById(userByName.getId());
        userInfoVO.setRolesInfos(userRoleById);
        return userInfoVO;
    }

    /**
     * 查看用户列表
     *
     * @param pageVO
     * @return
     */
    @Override
    public PageInfo<Map<String, Object>> listUserInfo(PageVO pageVO) {
        PageHelper.startPage(pageVO.getPageNum(), pageVO.getPageSize());
        List<Map<String, Object>> userInfoList = userInfoMapper.listUserInfo();
        return PageInfo.of(userInfoList);
    }

    /**
     * 获取B站个人信息
     *
     * @return
     */
    @Override
    public Map<String, Object> getBilibiliInfo(String mid) {
        Map<String, Object> map = BiliBiliUtil.GetUPMasterInformation(mid);
        map.put("fansfttention", BiliBiliUtil.GetFansFttention(mid));
        // 获取播放数和阅读量需要cookie，不能直接请求
        // map.put("readingstotalplays", BiliBiliUtil.getReadingsTotalPlays(mid));
        return map;
    }
}
